package com;
import com.csvreader.*;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by zybang on 2017/12/11.
 */
public class csv {
    public static void main(String args[]){
        String path1 = "E:\\a\\check.csv";
        String path2 = "E:\\a\\checkR.csv";
        String targetFile1 = "E:\\a\\null.csv";
        String targetFile2 = "E:\\a\\uiddiff.csv";
        String targetFile3 = "E:\\a\\coupondiff.csv";

        CsvWriter write1 =new CsvWriter(targetFile1,',', Charset.forName("UTF-8"));
        CsvWriter write2 =new CsvWriter(targetFile2,',', Charset.forName("UTF-8"));
        CsvWriter write3 =new CsvWriter(targetFile3,',', Charset.forName("UTF-8"));
        write1.setForceQualifier(true);
        write2.setForceQualifier(true);
        write3.setForceQualifier(true);
        try {
            // 创建CSV读对象
            CsvReader check1 = new CsvReader(path1);
            CsvReader check2 = new CsvReader(path2);

            Map R = new HashMap();
            Map T = new HashMap();

            //TEST
            while (check1.readRecord()){
                String s = check1.getRawRecord();
                String[] ss = s.split(",");
                T.put(ss[1], ss);

            }
            System.out.println("end!");

            //RD
            while (check2.readRecord()){
                String str = check2.getRawRecord();
                String[] strs = str.split(",");
                String[] ts = (String[]) T.get(strs[1]);
                //没查到
                if(ts == null ){
                    System.out.println("null: "+strs[1]);
                    write1.writeRecord(strs);
                }

                else{
                    if(strs.length == ts.length) {
                        //uid不同
                        if (!strs[0].equals(ts[0])) {
                            System.out.println("uid: "+strs[1]);
                            write2.writeRecord(strs);
                        }
                        //优惠券
                        if(strs.length == 3){
                            if(!strs[2].equals(ts[2])){
                                System.out.println("coupon:"+strs[1]);
                                write3.writeRecord(strs);
                            }
                        }
                    }else{
                        System.out.println("coupon: "+strs[1]);
                        write3.writeRecord(strs);
                    }


                }

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        write1.close();
        write2.close();
        write3.close();
    }
}
